const express = require('express');
const cors = require('cors');
const cookieParser = require('cookie-parser');
const mongoose = require('mongoose');
const authRoutes = require('./routes/authRoutes');
const config = require('./config');

const app = express();

// 中间件
app.use(cors({
  origin: 'http://localhost:5173', // 前端开发服务器
  credentials: true // 允许携带凭证
}));
app.use(express.json());
app.use(cookieParser());

// 路由
app.use('/api/auth', authRoutes);

// 连接数据库并启动服务器
mongoose.connect(config.dbUri)
  .then(() => {
    console.log('数据库连接成功');
    app.listen(5000, () => {
      console.log('服务器运行在 http://localhost:5000');
    });
  })
  .catch(err => {
    console.error('数据库连接失败:', err);
  }); 